package _interview75;

import java.util.*;

/**
 * 1657. 确定两个字符串是否接近
 */
public class No1657 {
    public boolean closeStrings(String word1, String word2) {
        int n1 = word1.length();
        int n2 = word2.length();
        if (n1 != n2) return false;
        HashMap<Character, Integer> map1 = new HashMap<>();
        for (int i = 0; i < n1; i++) {
            char key = word1.charAt(i);
            map1.put(key, map1.getOrDefault(key, 0) + 1);
        }
        HashMap<Character, Integer> map2 = new HashMap<>();
        for (int i = 0; i < n2; i++) {
            char key = word2.charAt(i);
            if (!map1.containsKey(key)) return false;
            else map2.put(key, map2.getOrDefault(key, 0) + 1);
        }
        List<Integer> list1 = map1.values().stream().sorted().toList();
        List<Integer> list2 = map2.values().stream().sorted().toList();
        for (int i = 0; i < list1.size(); i++) {
            if (!Objects.equals(list1.get(i), list2.get(i))) return false;
        }
        return true;
    }
}
